import { ConfigEnv, UserConfig, defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

const root = process.cwd()

export default ({ command, mode }: ConfigEnv): UserConfig => {
	let env = {} as any
	const isBuild = command === 'build'
	if (!isBuild) {
		env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
	} else {
		env = loadEnv(mode, root)
	}
	
	return {
		base: env.VITE_BASE_PATH,
		plugins: [
			vue(),
			VueSetupExtend(),
			AutoImport({
				resolvers: [ElementPlusResolver()]
			}),
			Components({
				resolvers: [ElementPlusResolver()]
			})
		],
		optimizeDeps: {
			include: ['schart.js']
		},
		resolve: {
			alias: {
				'@': '/src',
				'~': '/src/assets'
			}
		},
		define: {
			__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: "true",
		},
		server: {
			port: env.VITE_PORT,
			proxy: {
				'/api': {
					target: env.VITE_API_URL,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/api/, '')
				}
			}
		}
	}
}
